﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MathLib;
using System.Data.OleDb;
using System.IO;


namespace Demog {
    public partial class PopulationForm : Form {
        private bool fileOpened;
        private string strfileName;
        private Matrix file;

        public PopulationForm() {
            InitializeComponent();
        }



        private void PrintMatrix(DataGridView dgv, Matrix m) {
            DataTable table = CreateTable(m.NoRows, m.NoCols);
            for (int i = 0; i < m.NoRows; i++) {
                DataRow dr = table.Rows[i];
                for (int j = 0; j < m.NoCols; j++)
                    dr[j] = m[i, j];
            }

            dgv.DataSource = table;
        }

        private DataTable CreateTable(int rows, int columns) {
            DataTable t = new DataTable();
            for (int j = 0; j < columns; j++)
                t.Columns.Add();
            for (int i = 0; i < rows; i++)
                t.Rows.Add();

            return t;
        }

        private void bModeling_Click(object sender, EventArgs e) {
            AppParameters app = new AppParameters();
            try {
                app.PopulationsCount = int.Parse(tbPopulationsCount.Text);
                app.MaxFertility = double.Parse(tbMaxFertility.Text);
                app.GroupCount = int.Parse(tbGroupCount.Text);
                app.PopValue = double.Parse(tbPopValue.Text);
            } catch (Exception ex) {
                MessageBox.Show("Заполните все поля корректно", "Внимание", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            if (app.MaxFertility >= 1) {
                MessageBox.Show("Максимальный коэффициент рождаемости должен быть меньше 1.", "Внимание", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            AppParameters.Params = app;

            Matrix initPop = new Matrix(app.GroupCount, 1);
            if (fileOpened) {
            Matrix fertility = new Matrix(app.GroupCount,1);
            Matrix mortality = new Matrix(app.GroupCount,1);

                for (int i = 0; i < file.NoRows; i++) {
                    initPop[i, 0] = file[i, 0];
                    fertility[i, 0] = file[i, 1];
                    mortality[i, 0] = file[i, 2];
                }

                initPop = GenerationUtils.GenerateInit(app.PopValue, app.GroupCount);
                GeneratedPopulation genPop = GenerationUtils.RealPopulation(app.PopulationsCount, initPop, fertility, mortality);
                SimulatedPopulation simPop = new SimulatedPopulation(genPop);
                simPop.Show();

                } else {


                initPop = GenerationUtils.GenerateInit(app.PopValue, app.GroupCount);
                GeneratedPopulation genPop = GenerationUtils.GeneratePopulation(app.PopulationsCount, app.MaxFertility, initPop);
                SimulatedPopulation simPop = new SimulatedPopulation(genPop);
                simPop.Show();

                }

        }

        private void bClose_Click(object sender, EventArgs e) {
            Close();
        }

        private void OpenToolStripMenuItem_Click(object sender, EventArgs e) {
        }

        private void начальноеСостояниеToolStripMenuItem_Click(object sender, EventArgs e) {
            // If a file is not opened, then set the initial directory to the
            // FolderBrowserDialog.SelectedPath value.
            if (!fileOpened) {
                openFileDialog1.InitialDirectory = folderBrowserDialog1.SelectedPath;
                openFileDialog1.FileName = null;
            }

            // Display the openFile dialog.
            DialogResult result = openFileDialog1.ShowDialog();

            // OK button was pressed.
            if (result == DialogResult.OK) {
                strfileName = openFileDialog1.InitialDirectory + openFileDialog1.FileName;
                try {
                    // Output the requested file in richTextBox1.
                    Stream s = openFileDialog1.OpenFile();
                    s.Close();

                    fileOpened = true;

                } catch (Exception exp) {
                    MessageBox.Show("An error occurred while attempting to load the file. The error is:"
                                    + System.Environment.NewLine + exp.ToString() + System.Environment.NewLine);
                    fileOpened = false;
                }
                Invalidate();
                if (fileOpened)
                    MessageBox.Show("Файл загружен: " + strfileName);
                tbPopValue.Enabled = !fileOpened;
                tbGroupCount.Enabled = !fileOpened;
                tbPopulationsCount.Text = "20";
                tbMaxFertility.Text = "0";
               // tbPopulationsCount.Enabled = !fileOpened;
                tbMaxFertility.Enabled = !fileOpened;
                label5.Enabled = !fileOpened;
                label1.Enabled = !fileOpened;
                // closeMenuItem.Enabled = fileOpened
                if (fileOpened) {
                    IOUtilities iou = new IOUtilities();
                    DataTable dt = iou.LoadToDataTable(strfileName);
                    file = iou.ReadMatrixFromTable(dt);

                    double sum = 0;
                for (int i = 0; i < file.NoRows; i++) {
                    sum += file[i, 0];
                }

               double  PopValue = sum / file.NoRows;

                   tbPopValue.Text = PopValue.ToString();
                   tbGroupCount.Text = file.NoRows.ToString();
                }
            }

            // Cancel button was pressed.
            else if (result == DialogResult.Cancel) {
                return;
            }

        }

    }
}